﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace UMLDesigner
{
    partial class FormControl : UserControl
    {
        private ClassView editionClass;
        private ClassData classData;
        private List<AttributeData> temporalAttributes=new List<AttributeData>();

        public FormControl()
        {
            InitializeComponent();
            comboVisibility.DataSource = VisibilityType.GetTypes();
            comboModifiers.DataSource = ModifiersTypes.GetTypes();
        }

        private void buttonAddAtributes_Click(object sender, EventArgs e)
        {
            AttributesWindow attribute = new AttributesWindow();
            attribute.Show();
        }

        private void btnModifyAttributes_Click(object sender, EventArgs e)
        {
            if (listBoxAttributes.SelectedItem != null)
            {
                int index = listBoxAttributes.SelectedIndex;
                AttributesWindow addAttributes = new AttributesWindow(temporalAttributes[index]);
                addAttributes.Show();
            }
        }

        public void Refresh(ClassView classView)
        {
            editionClass = classView;
            classData = classView.GetClassData();

            txtClassName.Text =  classData.GetName();

            comboVisibility.SelectedItem = classData.GetVisibility();
            comboModifiers.SelectedItem = classData.GetModifier();

            listBoxAttributes.Items.Clear();
            foreach(AttributeData attr in classData.GetAttributes())
            {
                listBoxAttributes.Items.Add(attr.GetUMLRepresentation());
            }

            temporalAttributes.Clear();
            for (int i = 0; i < classData.GetAttributes().Count; i++)
            {
                temporalAttributes.Add(classData.GetAttributes()[i]);
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (editionClass != null)
            {
                classData.SetName(txtClassName.Text);
                classData.SetAttributes(temporalAttributes);
                classData.SetModifier(comboModifiers.Text);
                classData.SetVisibility(comboVisibility.Text);

                editionClass.Refresh();
            }
        }

        public void Deselect()
        {
            txtClassName.Text = "";
            editionClass = null;
        }
       
        public void AddAttribute(string name,string type,string visibility)
        {
            AttributeData attribute = new AttributeData(name,type,visibility);
            listBoxAttributes.Items.Add(attribute.GetUMLRepresentation());
            temporalAttributes.Add(attribute);           
        }

        public void ModifyAttribute(string name, string type, string visibility)
        {
            int index = listBoxAttributes.SelectedIndex;
            temporalAttributes[index].SetName(name);
            temporalAttributes[index].SetType(type);
            temporalAttributes[index].SetVisibility(visibility);
            listBoxAttributes.Items[index] = temporalAttributes[index].GetUMLRepresentation();
        }

        private void btnDeleteAtrribute_Click(object sender, EventArgs e)
        {
            if (listBoxAttributes.SelectedItem != null)
            {
                for (int i = 0; i < temporalAttributes.Count; i++)
                {
                    if (temporalAttributes[i].GetUMLRepresentation() == listBoxAttributes.Text)
                    {
                        temporalAttributes.Remove(temporalAttributes[i]);
                    }
                }
              
                listBoxAttributes.Items.Remove(listBoxAttributes.SelectedItem);
            }
        }

    }
}
